package at.bestsolution.e4.ui.workbench.renderers.jfx; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import org.eclipse.core.commands.ParameterizedCommand; import org.eclipse.e4.core.commands.ECommandService; import org.eclipse.e4.core.commands.EHandlerService; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.ui.model.application.commands.MParameter; import org.eclipse.e4.ui.model.application.ui.MElementContainer; import org.eclipse.e4.ui.model.application.ui.MUIElement; import org.eclipse.e4.ui.model.application.ui.MUILabel; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.model.application.ui.menu.MHandledItem; import org.eclipse.e4.ui.model.application.ui.menu.MItem; import org.eclipse.e4.ui.model.application.ui.menu.MToolItem; import org.eclipse.emf.common.util.URI; @SuppressWarnings("restriction") public class ToolItemRenderer extends JFXPartRenderer { private ParameterizedCommand generateParameterizedCommand( final MHandledItem item, final IEclipseContext lclContext) { ECommandService cmdService = (ECommandService) lclContext .get(ECommandService.class.getName()); Map<String, Object> parameters = null; List<MParameter> modelParms = item.getParameters(); if (modelParms != null && !modelParms.isEmpty()) { parameters = new HashMap<String, Object>(); for (MParameter mParm : modelParms) { parameters.put(mParm.getName(), mParm.getValue()); } } ParameterizedCommand cmd = cmdService.createCommand(item.getCommand() .getElementId(), parameters); item.setWbCommand(cmd); return cmd; } @Override public Object createWidget(MUIElement element) { Button button = new Button(); button.getStyleClass().add("toolbarButton"); MToolItem item = (MToolItem) element; String uri = item.getIconURI(); if( uri != null ) { try { URL url = new URL(URI.createURI(uri).toString()); ImageView icon = new ImageView(new Image(url.openStream())); button.setGraphic(icon); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return button; } public void hookControllerLogic(MUIElement me) { if (me instanceof MHandledItem) { final MHandledItem item = (MHandledItem) me; final IEclipseContext lclContext = getContext(me); Button l = (Button) item.getWidget(); l.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { EHandlerService service = (EHandlerService) lclContext .get(EHandlerService.class.getName()); ParameterizedCommand cmd = item.getWbCommand(); if (cmd == null) { cmd = generateParameterizedCommand(item, lclContext); } if (cmd == null) { System.err.println( "Failed to execute: " + item.getCommand()); return; } lclContext.set(MItem.class.getName(), item); service.executeHandler(cmd); lclContext.remove(MItem.class.getName()); } }); } } @Override protected Object getParentWidget(MUIElement element) { // TODO Auto-generated method stub return null; } @Override public void disposeWidget(MUIElement part) { // TODO Auto-generated method stub } @Override public void childRendered(MElementContainer<MUIElement> parentElement, MUIElement element) { // TODO Auto-generated method stub } @Override protected Object getImage(MUILabel element) { // TODO Auto-generated method stub return null; } @Override protected boolean requiresFocus(MPart element) { // TODO Auto-generated method stub return false; } }